/**
 * 
 */
package com.wegax.naming.interceptor;

import java.util.List;
import java.util.Map;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;

import com.wegax.naming.register.NameRegister;

/**
 * @author wegax
 * 
 */
class UserNameSetterHttpHeaderInterceptor extends org.apache.cxf.phase.AbstractPhaseInterceptor<Message> {

	private final NameRegister nameRegister;

	public UserNameSetterHttpHeaderInterceptor(NameRegister nameRegister) {
		super(Phase.PRE_INVOKE);
		this.nameRegister = nameRegister;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
	 */
	@Override
	public void handleMessage(Message message) throws Fault {
		Map<String, List<?>> headers = (Map<String, List<?>>) message.get(Message.PROTOCOL_HEADERS);
		if (headers.containsKey("http_user_name")) {
			List<?> httpUserNameHeaderList = headers.get("http_user_name");
			if (httpUserNameHeaderList != null && httpUserNameHeaderList.size() > 0) {
				nameRegister.setName((String) httpUserNameHeaderList.iterator().next());
			}
		}
	}

}
